//
//  DBJSAndHFeedbackVC.m
//  DBJ
//
//  Created by 肖浩然的mac on 2016/11/13.
//  Copyright © 2016年 itcast. All rights reserved.
//
#define Num 30;
#import "DBJSAndHFeedbackVC.h"

static NSString *FeedbackVCCell = @"FeedbackVCCell";

@interface DBJSAndHFeedbackVC ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>

//输入框

@property (weak, nonatomic) IBOutlet UITextView *textView;

//广告敬语
@property (weak, nonatomic) IBOutlet UILabel *WelcomeLabel;

//请联系我们
@property (weak, nonatomic) IBOutlet UITableViewCell *CallMeCell;

//用户交流群
@property (weak, nonatomic) IBOutlet UITableViewCell *ExchangeCell;

//
@property (weak, nonatomic) IBOutlet UILabel *textLabel;


@end

@implementation DBJSAndHFeedbackVC

//加载SB
-(instancetype)init{
    if (self = [super init]) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"DBJSAndHFeedbackVC" bundle:nil];
        self = [story instantiateInitialViewController];
    }
    return self;
}
-(void)dbjSetupSubviews{
  //    self.view.backgroundColor = [UIColor redColor];
    //初始化界面
    [self setupUI];
    //关闭弹簧效果
    self.tableView.bounces = NO;
}

#pragma mark-初始化界面
-(void)setupUI{
    //1.设置textView
    self.textView.text = @"请输入您宝贵的建议";
    self.textView.textColor = [UIColor grayColor];
    //设置textView的代理
    self.textView.delegate = self;
}

#pragma mark-实现textView的代理方法

//2.开始编辑时的方法
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.textView.textColor = [UIColor blackColor];
    if ([self.textView.text isEqualToString:@"请输入您宝贵的建议"]) {
        self.textView.text = @"";
    }
}

//3.在结束编辑的代理方法中进行如下操作
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length<1) {
        textView.text = @"请输入您宝贵的建议";
    }
}
//4.当textView发生改变的方法
-(void)textViewDidChange:(UITextView *)textView{
    NSInteger length = 30 - self.textView.text.length;
    //设置提示用语
    self.textLabel.text = [NSString stringWithFormat:@"还能输入%zd个字",length];
    self.textLabel.textColor = length>=0 ?[UIColor blackColor] :[UIColor redColor];
    self.textLabel.text = length>=0 ?[NSString stringWithFormat:@"还能输入%zd个字",length] : [NSString stringWithFormat:@"已经超出%zd个字",length];
}

//输入完毕隐藏键盘
- (void)viewDidLoad{
    [super viewDidLoad];
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
}

//4.1手势的实现
-(void)viewTapped:(UITapGestureRecognizer*)tapGr{
    [self.textView  resignFirstResponder];
}

-(void)dbjSetupNavigationItems{
    //右边Item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightItem)];
}

//右边提交的点击事件
-(void)clickRightItem{
    NSInteger length = self.textView.text.length;
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler: nil];
    
    if (length <=30 && length != 0) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"正在进行中...." preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
        //设置时间断点
        [self performSelector:@selector(disMiss:) withObject:alertController afterDelay:1.5];
    }else if (length == 0){

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"亲!不能空内容上传" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else{
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"亲,不要捣乱!" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}

//perform的事件
-(void)disMiss:(UIAlertController*)VC{
    [VC dismissViewControllerAnimated:YES completion:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - tableView的代理方法
/** 设置分组style的头间隔 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

@end
